package cn.hhchat.wp.dto.msg;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.Timestamp;
import java.util.Date;

/**
 * Created this one by HMH on 2017/6/15.
 */
@Data
@NoArgsConstructor
public class RMsg <T>{

    private int errcode;
    private String errmsg;
    private T data;
    private Timestamp createTime;

    public RMsg(int code,String errmsg, T data){
        this.errcode = code;
        this.errmsg = errmsg;
        this.data = data;
        this.createTime = new Timestamp(new Date().getTime());
    }


    public RMsg(ResultEnum resultEnum){
        this(resultEnum.getErrcode(), resultEnum.getErrmsg(), null);
    }
    
    public RMsg(ResultEnum resultEnum,T data) {
        this(resultEnum.getErrcode(), resultEnum.getErrmsg(), data);
    }

    public RMsg(ResultEnum resultEnum,String errmsg, T data) {
        this(resultEnum.getErrcode(), errmsg, data);
    }

    public RMsg setErrMsg(String errmsg) {
        this.errmsg = errmsg;
        return this;
    }

    public RMsg setErrCode(int errcode) {
        this.errcode = errcode;
        return this;
    }

    @JsonIgnore
    public boolean hasError() {
        return this.errcode != 0;
    }

    @JsonIgnore
    public boolean isRight() {
        return this.errcode == 0;
    }
}
